package com.itheima.controller;


import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSON;
import com.itheima.constant.MessageConstant;
import com.itheima.constant.RedisMessageConstant;
import com.itheima.entity.Result;
import com.itheima.pojo.Member;
import com.itheima.service.MemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import redis.clients.jedis.JedisPool;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.Map;

@RestController
@RequestMapping("/member")
public class MemberController {

    @Reference
    private MemberService memberService;

    @Autowired
    private JedisPool jedisPool;

    @RequestMapping("/login")
    public Result login(HttpServletResponse response, @RequestBody Map map){
        String telephone = (String) map.get("telephone");
        // 根据电话查询是否是会员
        Member member = memberService.findMemberByTelephone(telephone);
        if (member == null){
            return new Result(false,"请先注册会员");
        };
        //1.比对用户输入的验证码是否正确
        //1.1用户输入的验证码
        String validateCode = (String) map.get("validateCode");
        //1.2redis中去取的验证码
        String redisValidateCode = jedisPool.getResource().get(telephone + RedisMessageConstant.SENDTYPE_LOGIN);
        if(validateCode!=null && redisValidateCode!=null && validateCode.equals(redisValidateCode)){

            //将用户存入redis,用于验证
            Cookie cookie = new Cookie("login_member_telephone",telephone);
            cookie.setPath("/");
            cookie.setMaxAge(60*60*24*30);
            response.addCookie(cookie);

            String  memberJson= JSON.toJSON(member).toString();
            jedisPool.getResource().setex(telephone,60*30,memberJson);
            return new Result(true, MessageConstant.LOGIN_SUCCESS);
        }else {
            return  new Result(false,MessageConstant.VALIDATECODE_ERROR);
        }
    }

    // 完善个人资料
    @RequestMapping("/perfectionData")
    public Result perfectionData(HttpServletResponse response, @RequestBody Map map){
        String telephone = (String) map.get("telephone");

        // 根据电话查询会员
        Member member = memberService.findMemberByTelephone(telephone);
        if(member == null){
            return new Result(false,"请先注册会员");
        }

        //1.比对用户输入的验证码是否正确
        //1.1用户输入的验证码
        String validateCode = (String) map.get("validateCode");
        //1.2redis中去取的验证码
        String redisValidateCode = jedisPool.getResource().get(telephone + RedisMessageConstant.SENDTYPE_EDIT);
        if(validateCode!=null && redisValidateCode!=null && validateCode.equals(redisValidateCode)){

            // 页面输入的密码
            String password = (String) map.get("password");
            // 页面输入的确认密码
            String passwordOk = (String) map.get("passwordOk");

            if (null != password && null != passwordOk && !password.equals(passwordOk)){
                return new Result(false,"密码确认不一样");
            }
           /* // md5加密密码
            password = MD5Utils.md5(password);
            // 后台获取密码,密码为md5格式
            String memberPasswordpassword = member.getPassword();
            if (memberPasswordpassword != null && !memberPasswordpassword.equals(password)){
                return new Result(false,"密码错误");
            }
            // 修改用MD5加密后的密码
            map.put("password",password);
            */
            try {
                memberService.edit(map);
            } catch (Exception e) {
                e.printStackTrace();
                return  new Result(false,MessageConstant.EDIT_MEMBER_FAIL);
            }

            //将用户存入redis,用于验证
            Cookie cookie = new Cookie("login_member_telephone",telephone);
            cookie.setPath("/");
            cookie.setMaxAge(60*60*24*30);
            response.addCookie(cookie);

            String  memberJson= JSON.toJSON(member).toString();
            jedisPool.getResource().setex(telephone,60*30,memberJson);

            return new Result(true, MessageConstant.EDIT_MEMBER_SUCCESS);
        }else {
            return  new Result(false,MessageConstant.VALIDATECODE_ERROR);
        }
    }

    // 注册会员
    @RequestMapping("/register")
    public Result register(HttpServletResponse response, @RequestBody Map map){
        String telephone = (String) map.get("telephone");
        // 根据号码查找用户
        Member member = memberService.findMemberByTelephone(telephone);

        if(member != null){
            // 如果不为空,证明注册过
            return new Result(false,"电话号码已注册过会员");
        }
        //1.比对用户输入的验证码是否正确
        //1.1用户输入的验证码
        String validateCode = (String) map.get("validateCode");
        //1.2redis中去取的验证码
        String redisValidateCode = jedisPool.getResource().get(telephone + RedisMessageConstant.SENDTYPE_REGISTER);
        if(validateCode!=null && redisValidateCode!=null && validateCode.equals(redisValidateCode)){
            // 获取邮箱验证码
            String emailCode = (String) map.get("emailCode");
            // 获取邮箱
            String email = (String) map.get("email");
            //redis中去取的邮箱验证码
            String redisEmailCode = jedisPool.getResource().get(email);
            if (null != emailCode && null != redisEmailCode && emailCode.equals(redisEmailCode)){
                // 邮箱验证码如果都一样
            }else {
                // 邮箱验证码如果不一样
                return new Result(false,MessageConstant.EMAIL_VALIDATECODE_ERROR);
            }

            member = new Member();
            member.setRegTime(new Date());
            member.setPhoneNumber(telephone);
            member.setEmail(email);

            try {
                memberService.add(member);
            } catch (Exception e) {
                e.printStackTrace();
                return new Result(true, MessageConstant.ADD_MEMBER_FAIL);
            }

            Cookie cookie = new Cookie("login_member_telephone",telephone);
            cookie.setPath("/");
            cookie.setMaxAge(60*60*24*30);
            response.addCookie(cookie);

            String  memberJson= JSON.toJSON(member).toString();
            jedisPool.getResource().setex(telephone,60*30,memberJson);
            return new Result(true, MessageConstant.ADD_MEMBER_SUCCESS);
        }else {
            return  new Result(false,MessageConstant.VALIDATECODE_ERROR);
        }
    }
}
